package com.bootdo.common.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;

import com.bootdo.common.utils.Hog;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

 
@Configuration
//这个注解代表加载好 数据源的类之后再加载当前类
@AutoConfigureAfter(DruidDBConfig.class)
//@MapperScan(basePackages="com.bootdo.**.domain")
public class MybatisConfiguration {
 
	private static Logger log = LoggerFactory.getLogger(MybatisConfiguration.class);

 
	//XxxMapper.xml文件所在路径
	@Value("${spring.datasource.mapperLocations}")
    private String mapperLocations;
    //加载全局的配置文件
    @Value("${spring.datasource.configLocation}")
    private String configLocation;
    //加载全局的配置文件
    @Value("${spring.datasource.typeAliasesPackage}")
    private String typeAliasesPackage;
	@Autowired
	@Qualifier("writeDataSource01")
	private DataSource writeDataSource;
	@Autowired
	@Qualifier("readDataSource01")
	private DataSource readDataSource01;
 
	
	 /**
     * 多数据源需要自己设置sqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(routingDataSource());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 实体类对应的位置
        Hog.i("typeAliasesPackage - >"+typeAliasesPackage);
        bean.setTypeAliasesPackage(typeAliasesPackage);
        // mybatis的XML的配置
        Hog.i("mapperLocations - >"+mapperLocations);
        Hog.i("configLocation - >"+configLocation);
        bean.setMapperLocations(resolver.getResources(mapperLocations));     
        //配置类 
        org.apache.ibatis.session.Configuration c = new org.apache.ibatis.session.Configuration();
        c.setLogImpl(com.bootdo.common.dataimp.MybatilsLogImp.class);
        //是否开启大小写驼峰转换
        c.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(c);
        //bean.setConfigLocation(resolver.getResource(configLocation));
        return bean.getObject();
    }

    
    /**
     * 设置事务，事务需要知道当前使用的是哪个数据源才能进行事务处理
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        return new DataSourceTransactionManager(routingDataSource());
    }

    /**
     * 设置数据源路由，通过该类中的determineCurrentLookupKey决定使用哪个数据源
     */
    @Bean
    public AbstractRoutingDataSource routingDataSource() {
        MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DbContextHolder.WRITE, writeDataSource);
        targetDataSources.put(DbContextHolder.READ+1, readDataSource01);
        proxy.setDefaultTargetDataSource(writeDataSource);
        proxy.setTargetDataSources(targetDataSources);
        return proxy;
    }
	
    
}
